package com.youlai.boot.modules.brand.controller;

import com.youlai.boot.modules.brand.model.BrandForm;
import com.youlai.boot.modules.brand.model.BrandPageQuery;
import com.youlai.boot.modules.brand.model.BrandVO;
import com.youlai.boot.modules.brand.service.BrandService;
import com.youlai.boot.modules.merchant.service.MerchantService;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

import com.youlai.boot.modules.merchant.model.MerchantPageQuery;
import com.youlai.boot.modules.merchant.model.MerchantVO;

@Tag(name = "19, 品牌管理", description = "品牌管理")
@RestController
@RequestMapping("/api/v1/brand")
@RequiredArgsConstructor
public class BrandController {

    private final BrandService brandService;
    private final MerchantService merchantService;

    @Operation(summary = "品牌列表")
    @GetMapping("/page")
    public PageResult<BrandVO> getBrandPage(BrandPageQuery query) {
        IPage<BrandVO> result = brandService.getBrandPage(query);
        return PageResult.success(result);
    }

    @Operation(summary = "商户品牌列表")
    @GetMapping("/merchant/brands/page")
    public PageResult<BrandVO> getMerchantBrandPage(@Valid BrandPageQuery query) {
        IPage<BrandVO> page = brandService.getBrandsByMerchantId(query);
        return PageResult.success(page);
    }

    @Operation(summary = "品牌表单")
    @GetMapping("/{id}/form")
    public Result<BrandForm> getForm(@PathVariable Long id) {
        BrandForm form = brandService.getBrandFormById(id);
        return Result.success(form);
    }

    @Operation(summary = "新增品牌")
    @PostMapping
    public Result<Boolean> save(@RequestBody BrandForm form) {
        boolean result = brandService.save(form);
        return Result.judge(result);
    }

    @Operation(summary = "编辑品牌")
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody BrandForm form) {
        boolean result = brandService.update(id, form);
        return Result.judge(result);
    }

    @Operation(summary = "删除品牌")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = brandService.delete(id);
        return Result.judge(result);
    }

    @Operation(summary = "品牌商户列表，关联查询")
    @GetMapping("/{id}/merchants")
    public PageResult<MerchantVO> getMerchants(@PathVariable Long id, @Valid MerchantPageQuery query) {
        IPage<MerchantVO> page = merchantService.getMerchantPageByBrandId(id, query);
        return PageResult.success(page);
    }
}
